﻿using System.Windows;
using System.Windows.Controls;
using Notoric.ViewModel.Elements.CWNotation;

namespace Notoric.View.Music.CWNotation.TemplateSelectors.CWNotation
{
    /// <summary>
    /// Selects the <see cref="DataTemplate"/> for a <see cref="BarlineSegment"/> according
    /// to the <see cref="Barline.Type"/>.
    /// </summary>
    public class BarlineSegmentTemplateSelector : DataTemplateSelector
    {
        /// <inheritdoc/>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if (element != null && item != null && item is BarlineSegment)
            {
                BarlineSegment bs = item as BarlineSegment;

                switch (bs.FindOwner<Barline>().Type)
                {
                    case BarlineType.Single:
                        return element.FindResource("SingleBarlineTemplate") as DataTemplate;
                    case BarlineType.Double:
                        return element.FindResource("DoubleBarlineTemplate") as DataTemplate;
                    case BarlineType.Final:
                        return element.FindResource("FinalBarlineTemplate") as DataTemplate;
                    case BarlineType.RepetitionClose:
                        return element.FindResource("RepetitionCloseBarlineTemplate") as DataTemplate;
                    case BarlineType.RepetitionOpen:
                        return element.FindResource("RepetitionOpenBarlineTemplate") as DataTemplate;
                    case BarlineType.RepetitionBoth:
                        return element.FindResource("RepetitionBothBarlineTemplate") as DataTemplate;
                    default:
                        return null;
                }
            }

            return null;
        }
    }
}
